package com.gfm.story.controller;

import com.gfm.story.entity.base.PageBean;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.user.User;
import com.gfm.story.interceptor.UserContext;
import com.gfm.story.service.story.StoreService;
import com.gfm.story.service.user.UserService;
import com.gfm.story.util.ParamUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.controller
 * @Author: Administrator
 * @CreateTime: 2019-06-06 11:15
 * @Description: 小说收藏接口（书架）
 */
@Controller
@RequestMapping("Store")
public class StoreController {

    @Resource
    private StoreService storeService;

    @Resource
    private UserService userService;

    /**
     * 查询所有收藏
     * @param userId
     * @return
     */
    @RequestMapping("getUserInfo")
    public String getUserInfo(Long userId, ModelMap modelMap){
        User user = UserContext.getUser();
        if (user==null){
            modelMap.put("user", new User());
            //modelMap.put("error", "请重新登录");
            return "login";
        }{
            //根据
//            modelMap.put("StoreList", storeService.getAllStore(user.getId()));
//            System.out.println(modelMap.toString());
            return "mine";
        }
    }

    @RequestMapping("getStoreList")
    public String getStoreList(String nameOrAuthor,ModelMap modelMap){
        User user = UserContext.getUser();
        if (user==null){
            modelMap.put("user", new User());
            //modelMap.put("error", "请重新登录");
            return "login";
        }{
            //根据
            modelMap.put("StoreList", storeService.getAllStore(user.getId(),nameOrAuthor));
            return "sj";
        }
    }

    @RequestMapping("getTotalPage")
    public String getTotalPage(HttpServletRequest request,int index, PageBean pageBean,ModelMap modelMap) throws Exception {
        Map<String, Object> map = ParamUtil.getParameterMap(request);
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getRows());
        modelMap.put("bookList", storeService.getTotalPage(map,pageBean.getPage()));
        modelMap.put("index", index);
        return "ph";
    }


    /**
     * 根据书签id删除书签
     * @param storeId
     * @param modelMap
     * @return
     */
    @RequestMapping("remove")
    public String remove(Long storeId,ModelMap modelMap){
        storeService.remove(storeId);
        User user = UserContext.getUser();
        return "redirect:/Store/getStoreList?userId="+user.getId();
    }

    @RequestMapping("addOrEdit")
    @ResponseBody
    public ResultBean addOrEdit(Long bookId,Long userId,Long chapterId){
        if (bookId==null){
            return ResultBean.getSuccess("加入书签失败");
        }
        User user = UserContext.getUser();
        if (user==null){
            return ResultBean.getSuccess("加入书签失败,请先登录");
        }else{
            user=userService.getUserBySession(user);
            if (storeService.addOrEdit(bookId,user.getId(),chapterId)){
                return ResultBean.getSuccess("添加书签成功");
            }
            return ResultBean.getSuccess("加入书签失败");
        }

    }

}
